package com.bwie.service.impl;

import com.bwie.common.domain.Book;
import com.bwie.EasyPoiUtil;
import com.bwie.domain.BookExport;
import com.bwie.mapper.BookMapper;
import com.bwie.service.BookService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: BookServiceImpl
 * @Description: TODO
 * @version: v1.８.0
 * @author: GONGWENXUE
 * @date: 2025/11/10 11:22
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookMapper bookMapper;
    @Autowired
    EasyPoiUtil easyPoiUtil;
    @Autowired
    HttpServletResponse response;

    @Override
    public List<Book> bookList(Book book) {
        return bookMapper.bookList(book);
    }

    @Override
    public BookExport Import(MultipartFile file) {
        List<Book> bookList=bookMapper.getBookImport();
        ArrayList<BookExport> list = new ArrayList<>();
        for (Book book : bookList)  {
            BookExport bookExport = new BookExport();
            BeanUtils.copyProperties(book,bookExport);
            list.add(bookExport);
        }
        easyPoiUtil.exportExcel(response,list, BookExport.class,"列表");
        BookExport bookExport = new BookExport();
        //	字段前有红色*号的为必填字段，PISSN、PISBN、EISSN、EISBN至少填写一个
            String pisbn = bookExport.getPISBN();
            String pissn = bookExport.getPISSN();
            if(pissn==null ||pisbn==null){
                throw new RuntimeException("至少填写一个");
            }
        return bookExport;
    }

    @Override
    public List<BookExport> getPreview(BookExport bookExport) {
        return bookMapper.getPreview(bookExport);
    }

    @Override
    public List<BookExport> getChecking(BookExport bookExport) {
        return bookMapper.getChecking(bookExport);
    }
}
